﻿// // ******************************************************************
// //       /\ /|       @file       AutoDbContext.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-02-22 20:02
// //    *(__\_\        @Copyright  Copyright (c) , 2025-02-04 14:02 Await
// // ******************************************************************

using AutomaticSubtitles.Service.DataAccess.Model;

namespace AutomaticSubtitles.Service.DataAccess;

public class AutoDbContext : MasaDbContext
{
    public AutoDbContext(MasaDbContextOptions<AutoDbContext> options) : base(options)
    {
    }

    public DbSet<Auto_User> AutoUsers { get; set; }
    public DbSet<Auto_Config> AutoConfigs { get; set; }

    public DbSet<Auto_Aiconfig> AutoAiconfigs { get; set; }
    public DbSet<Auto_Language> AutoLanguages { get; set; }
    public DbSet<Auto_Media> AutoMedias { get; set; }
    public DbSet<Auto_Subtitle> AutoSubtitles { get; set; }
    public DbSet<Auto_CacheKey> AutoCacheKeys { get; set; }
    public DbSet<Auto_Translate> Auto_Translates { get; set; }
    public DbSet<Auto_LanguageConversion> Auto_LanguageConversions { get; set; }
    public DbSet<Auto_TranslationRecord> Auto_TranslationRecords { get; set; }
    public DbSet<Auto_FilterWords> Auto_FilterWords { get; set; }
    public DbSet<Auto_LanguageFilterWords> Auto_LanguageFilterWords { get; set; }

    protected override void OnModelCreatingExecuting(ModelBuilder modelBuilder)
    {
        base.OnModelCreatingExecuting(modelBuilder);
        ConfigEntities(modelBuilder);
    }

    private static void ConfigEntities(ModelBuilder builder)
    {
        builder.Entity<Auto_User>(options =>
        {
            options.ToTable("Auto_User");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id)
                .HasMaxLength(32).HasComment("主键");
            options.Property(x => x.NickName).HasMaxLength(30).IsRequired();
            options.HasIndex(x => x.Account).IsUnique();
            options.Property(x => x.Account).HasMaxLength(50).IsRequired();
            options.Property(x => x.Password).HasMaxLength(300).IsRequired();
            options.Property(x => x.Email).HasMaxLength(200);
            options.Property(x => x.Avatar).HasMaxLength(200);
            options.Property(x => x.NickName).HasMaxLength(50).IsRequired();
            options.Property(e => e.CreateTime).HasComment("添加时间");
            options.Property(e => e.UpdateTime).HasComment("更新时间");
            options.Property(e => e.RefreshToken).HasComment("刷新密钥");
        });
        builder.Entity<Auto_User>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_User>()
            .HasIndex(e => e.NickName);
        builder.Entity<Auto_User>()
            .HasIndex(e => e.Account);
        builder.Entity<Auto_User>()
            .HasIndex(e => e.Email);
        builder.Entity<Auto_User>()
            .HasIndex(e => e.RefreshToken);

        builder.Entity<Auto_Config>(options =>
        {
            options.ToTable("Auto_Config");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.Name).HasMaxLength(50).IsRequired();
            options.HasIndex(x => x.Name).IsUnique();
            options.Property(x => x.UserName).HasMaxLength(50);
            options.Property(x => x.Password).HasMaxLength(300);
            options.Property(x => x.ServerUrl).HasMaxLength(300).IsRequired();
            options.Property(x => x.UserId).HasMaxLength(300);
            options.Property(x => x.Token).HasMaxLength(500);
            options.Property(x => x.IsNormal);
            options.Property(x => x.Type);
            // options.Property(e => e.TokenExpireTimeDate).HasComment("更新时间");
        });
        builder.Entity<Auto_Config>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_Config>()
            .HasIndex(e => e.UserName);
        builder.Entity<Auto_Config>()
            .HasIndex(e => e.Password);
        builder.Entity<Auto_Config>()
            .HasIndex(e => e.ServerUrl);
        builder.Entity<Auto_Config>()
            .HasIndex(e => e.UserId);

        builder.Entity<Auto_Aiconfig>(options =>
        {
            options.ToTable("Auto_Aiconfig");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.Name).HasMaxLength(50).IsRequired();
            options.HasIndex(x => x.Name).IsUnique();
            options.Property(x => x.Token).HasMaxLength(300);
            options.Property(x => x.Url).HasMaxLength(300).IsRequired();
        });
        builder.Entity<Auto_Aiconfig>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_Aiconfig>()
            .HasIndex(e => e.Name);

        builder.Entity<Auto_Language>(options =>
        {
            options.ToTable("Auto_Language");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.Key).IsRequired();
            options.HasIndex(x => x.Key).IsUnique();
            options.Property(x => x.IsAudio);
            options.Property(x => x.IsSkip);
        });
        builder.Entity<Auto_Language>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_Language>()
            .HasIndex(e => e.Key);

        builder.Entity<Auto_Media>(options =>
        {
            options.ToTable("Auto_Media");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.Name).HasMaxLength(100);
            options.Property(x => x.Path).HasMaxLength(500);
            options.Property(x => x.MainRootDirFPath).HasMaxLength(500);
            options.Property(x => x.PosterAddress).HasMaxLength(500);
            options.Property(x => x.LandscapeAddress).HasMaxLength(500);
            options.Property(x => x.ParentLevelId).HasMaxLength(500);
            options.Property(x => x.InformationAddress).HasMaxLength(500);
            options.Property(x => x.Size);
            options.Property(x => x.UpdateTime);
            options.Property(x => x.LastWriteTime);
        });
        builder.Entity<Auto_Media>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_Media>()
            .HasIndex(e => e.Path);
        builder.Entity<Auto_Media>()
            .HasIndex(e => e.Name);

        builder.Entity<Auto_Subtitle>(options =>
        {
            options.ToTable("Auto_Subtitle");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.VideoId).HasMaxLength(32).HasComment("媒体id");
            options.Property(x => x.Name).HasMaxLength(100);
            options.Property(x => x.IsDefault);
            options.Property(x => x.UpdateTime);
            options.Property(x => x.Path).HasMaxLength(500);
            options.Property(x => x.Language).HasMaxLength(100);

            options.HasOne(t => t.AutoMedia)
                .WithMany(x => x.AutoSubtitles)
                .HasForeignKey(t => t.VideoId);
        });
        builder.Entity<Auto_Subtitle>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_Subtitle>()
            .HasIndex(e => e.Path);
        builder.Entity<Auto_Subtitle>()
            .HasIndex(e => e.Name);
        builder.Entity<Auto_Subtitle>()
            .HasIndex(e => e.VideoId);

        builder.Entity<Auto_CacheKey>(options =>
        {
            options.ToTable("Auto_CacheKey");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.Name).HasMaxLength(50).IsRequired();
            options.Property(x => x.Key).HasMaxLength(50).IsRequired();
            options.Property(x => x.GroupName).HasMaxLength(50).IsRequired();
            options.Property(x => x.Value).HasMaxLength(500).IsRequired();
            options.Property(x => x.SecondaryValue).HasMaxLength(500);
            options.Property(x => x.CreateTime);
        });
        builder.Entity<Auto_CacheKey>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_CacheKey>()
            .HasIndex(e => e.Name);
        builder.Entity<Auto_CacheKey>()
            .HasIndex(e => e.Key);
        builder.Entity<Auto_CacheKey>()
            .HasIndex(e => e.GroupName);

        builder.Entity<Auto_Translate>(options =>
        {
            options.ToTable("Auto_Translate");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.LanguageType).HasMaxLength(50).IsRequired();
            options.Property(x => x.Value).HasMaxLength(500).IsRequired();
            options.Property(x => x.TranslatedValue).HasMaxLength(500).IsRequired();
            options.Property(x => x.TranslatedLanguageType).HasMaxLength(50).IsRequired();
            options.Property(x => x.TriggerCount);
            options.Property(x => x.Source);
            options.Property(x => x.CreateTime);
            options.Property(x => x.SourcePath);
        });
        builder.Entity<Auto_Translate>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_Translate>()
            .HasIndex(e => e.LanguageType);
        builder.Entity<Auto_Translate>()
            .HasIndex(e => e.Value);
        builder.Entity<Auto_Translate>()
            .HasIndex(e => e.TranslatedValue);
        builder.Entity<Auto_Translate>()
            .HasIndex(e => e.TranslatedLanguageType);
        builder.Entity<Auto_Translate>()
            .HasIndex(e => e.SourcePath);

        builder.Entity<Auto_LanguageConversion>(options =>
        {
            options.ToTable("Auto_LanguageConversion");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.Language).HasMaxLength(50).IsRequired();
            options.Property(x => x.Suffix).HasMaxLength(50).IsRequired();
            options.Property(x => x.IsInclude);
        });
        builder.Entity<Auto_LanguageConversion>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_LanguageConversion>()
            .HasIndex(e => e.Language);
        builder.Entity<Auto_LanguageConversion>()
            .HasIndex(e => e.Suffix);

        builder.Entity<Auto_TranslationRecord>(options =>
        {
            options.ToTable("Auto_TranslationRecord");
            options.TryConfigureConcurrencyStamp();
            options.Property(x => x.Id).HasMaxLength(32).HasComment("主键");
            options.Property(x => x.MediaId).HasMaxLength(32).IsRequired().HasComment("媒体ID");
            options.Property(x => x.Name).HasMaxLength(100).IsRequired();
            options.Property(x => x.TranslatedLanguage).HasMaxLength(50).IsRequired();
            options.Property(x => x.TranslatedPath).HasMaxLength(500).IsRequired();
            options.Property(x => x.CreateTime).HasComment("创建时间");

            // 配置与媒体的关联
            options.HasOne(t => t.AutoMedia)
                .WithMany()
                .HasForeignKey(t => t.MediaId)
                .OnDelete(DeleteBehavior.Cascade);
        });
        builder.Entity<Auto_TranslationRecord>()
            .HasIndex(e => e.Id);
        builder.Entity<Auto_TranslationRecord>()
            .HasIndex(e => e.MediaId);
        builder.Entity<Auto_TranslationRecord>()
            .HasIndex(e => e.Name);
        builder.Entity<Auto_TranslationRecord>()
            .HasIndex(e => e.TranslatedLanguage);
        builder.Entity<Auto_TranslationRecord>()
            .HasIndex(e => e.TranslatedPath);

        var user = new Auto_User(BasicsHelper.GetId())
        {
            Account = "AwaitTranslation",
            NickName = "管理员",
            Password = DESUtils.Encrypt("AwaitTranslation"),
            IsAdmin = true,
            Email = "admin@AutomaticTranslationSystem.com",
            CreateTime = DateTime.Now
        };

        builder.Entity<Auto_User>().HasData(user);


        // var config = new Auto_Config(BasicsHelper.GetId())
        // {
        //     Name = "AwaitTranslation",
        //     Type = 0,
        //     UserName = "await",
        //     Password = "zd2580",
        //     ServerUrl = "http://127.0.0.1:8096",
        //     Token = "c8ff12ed31bc474c922dcf78504da20f"
        // };

        // builder.Entity<Auto_Config>().HasData(config);

        var cacheTVKey = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheTVKey,
            Key = CacheCommon.CacheTVKey,
            Value = "",
            GroupName = "媒体配置",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var mvCacheKey = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheMovieKey,
            Key = CacheCommon.CacheMovieKey,
            Value = "",
            GroupName = "媒体配置",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var noCacheKey = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheNoKey,
            Key = CacheCommon.CacheNoKey,
            Value = "",
            GroupName = "媒体配置",
            IsShow = true,
            CreateTime = DateTime.Now
        };

        var baiduTranslateApiKey = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.BaiduTranslateApiKey,
            Key = CacheCommon.BaiduTranslateApiKey,
            Value = "",
            SecondaryValue = "",
            GroupName = "百度翻译配置",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var task1 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheTaskKey,
            Key = CacheCommon.CacheTaskKey,
            SecondaryValue = CacheCommon.CacheKey,
            Value = CacheCommon.CacheKey,
            GroupName = "后台任务",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var task2 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheTaskKey,
            Key = CacheCommon.CacheTaskKey,
            SecondaryValue = CacheCommon.TranslateBackgroundServiceKey,
            Value = CacheCommon.TranslateBackgroundServiceKey,
            GroupName = "后台任务",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var task3 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheTaskKey,
            Key = CacheCommon.CacheTaskKey,
            SecondaryValue = CacheCommon.TranslationDataSynchBackgroundKey,
            Value = CacheCommon.TranslationDataSynchBackgroundKey,
            GroupName = "后台任务",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var task4 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.CacheTaskKey,
            Key = CacheCommon.CacheTaskKey,
            SecondaryValue = CacheCommon.AutoTranslationSubBackgroundServiceKey,
            Value = CacheCommon.AutoTranslationSubBackgroundServiceKey,
            GroupName = "后台任务",
            IsShow = true,
            CreateTime = DateTime.Now
        };


        builder.Entity<Auto_CacheKey>().HasData(cacheTVKey);
        builder.Entity<Auto_CacheKey>().HasData(mvCacheKey);
        builder.Entity<Auto_CacheKey>().HasData(noCacheKey);
        builder.Entity<Auto_CacheKey>().HasData(baiduTranslateApiKey);
        builder.Entity<Auto_CacheKey>().HasData(task1);
        builder.Entity<Auto_CacheKey>().HasData(task2);
        builder.Entity<Auto_CacheKey>().HasData(task3);
        builder.Entity<Auto_CacheKey>().HasData(task4);

        var ai1 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.AiConfig,
            Key = CacheCommon.AiConfig,
            SecondaryValue = "",
            Value = "",
            GroupName = "Ai配置",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var ai2 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.AiConfigValue,
            Key = CacheCommon.AiConfigValue,
            SecondaryValue = "false",
            Value = "",
            GroupName = "Ai配置",
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var ai3 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.AiConfigPromptWord,
            Key = CacheCommon.AiConfigPromptWord,
            Value = PromptWord,
            IsShow = true,
            GroupName = "Ai配置",

            CreateTime = DateTime.Now
        };
        builder.Entity<Auto_CacheKey>().HasData(ai1);
        builder.Entity<Auto_CacheKey>().HasData(ai2);
        builder.Entity<Auto_CacheKey>().HasData(ai3);

        var ai4 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.Configordering,
            Key = CacheCommon.Configordering,
            Value = GlobalVariableEnumConfiguration.TranslationConfigurationEnum.AI.ToString(),
            GroupName = "优先排序",
            Sort = 1,
            IsShow = true,
            CreateTime = DateTime.Now
        };
        var ai5 = new Auto_CacheKey(BasicsHelper.GetId())
        {
            Name = CacheCommon.Configordering,
            Key = CacheCommon.Configordering,
            Value = GlobalVariableEnumConfiguration.TranslationConfigurationEnum.Baidu.ToString(),
            GroupName = "优先排序",
            Sort = 2,
            IsShow = true,
            CreateTime = DateTime.Now
        };
        builder.Entity<Auto_CacheKey>().HasData(ai4);
        builder.Entity<Auto_CacheKey>().HasData(ai5);

        builder.Entity<Auto_LanguageConversion>().HasData(new Auto_LanguageConversion(BasicsHelper.GetId())
        {
            Language = "zh",
            Suffix = "chi",
            IsInclude = true
        });

        builder.Entity<Auto_LanguageConversion>().HasData(new Auto_LanguageConversion(BasicsHelper.GetId())
        {
            Language = "zh",
            Suffix = "zht"
        });

        builder.Entity<Auto_LanguageConversion>().HasData(new Auto_LanguageConversion(BasicsHelper.GetId())
        {
            Language = "en",
            Suffix = "eng",
            IsInclude = true
        });

        // 配置过滤词实体
        builder.Entity<Auto_FilterWords>(options =>
        {
            options.ToTable("Auto_FilterWords");
            options.HasKey(x => x.Id);
            options.Property(x => x.Id).HasMaxLength(50).IsRequired().HasComment("主键ID");
            options.Property(x => x.Word).HasMaxLength(255).IsRequired().HasComment("过滤词");
            options.Property(x => x.IsActive).IsRequired().HasComment("是否启用");
            options.Property(x => x.CreateTime).IsRequired().HasComment("创建时间");
            options.Property(x => x.Description).HasMaxLength(500).HasComment("描述/备注");
        });

        // 配置特定语言过滤词实体
        builder.Entity<Auto_LanguageFilterWords>(options =>
        {
            options.ToTable("Auto_LanguageFilterWords");
            options.HasKey(x => x.Id);
            options.Property(x => x.Id).HasMaxLength(50).IsRequired().HasComment("主键ID");
            options.Property(x => x.Language).HasMaxLength(10).IsRequired().HasComment("语言代码");
            options.Property(x => x.Word).HasMaxLength(255).IsRequired().HasComment("过滤词");
            options.Property(x => x.IsActive).IsRequired().HasComment("是否启用");
            options.Property(x => x.CreateTime).IsRequired().HasComment("创建时间");
            options.Property(x => x.Description).HasMaxLength(500).HasComment("描述/备注");

            // 创建索引以提高查询效率
            options.HasIndex(x => x.Language);
        });
    }
}